//
//  ToastView.swift
//  Game
//
//  Created by woniu on 2025/8/28.
//

import Foundation
import UIKit

public let kScreenW = UIScreen.main.bounds.size.width
public let kScreenH = UIScreen.main.bounds.size.height

class ToastView: UIView {
    
    private var msgLab:UILabel!
    public let kOnToastDismiss = "kOnToastDismiss"
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    ///增加通知监听
    static func register(_ obver:Any,_ sel:Selector,_ name:String){
        NotificationCenter.default.addObserver(obver, selector: sel, name: NSNotification.Name(rawValue:name), object: nil)
    }
    
    ///发送通知(广播)
    static func sendNotify(_ name:String,_ obj:Any?){
        NotificationCenter.default.post(name: NSNotification.Name(name), object: obj)
    }
    
    private func setupUI() {
        ToastView.register(self, #selector(onDismiss), kOnToastDismiss)
        self.backgroundColor = UIColor.black.withAlphaComponent(0.8)
        msgLab = UILabel()
        msgLab.numberOfLines = 0
        msgLab.textAlignment = .center
        msgLab.textColor = .white
        msgLab.font = UIFont.systemFont(ofSize: 16)
        self.addSubview(msgLab)
    }
    
    @objc func onDismiss(){
        self.removeFromSuperview()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        msgLab.frame = self.bounds
    }
    
    private func getTxtSize(msg:String,maxSize:CGSize)->CGSize{
        let realSize = msg.boundingRect(with:maxSize, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font:msgLab.font!], context: nil).size
        return CGSize(width: realSize.width + 30, height: realSize.height+30)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}
extension ToastView{
    
    public class func show(_ msg:String){
        //删除已正在显示的其他通知
        ToastView.sendNotify("kOnToastDismiss",nil)
        
        let alert = ToastView(frame: .zero)
        
        let maxSize = CGSize(width: kScreenW-80, height: kScreenH-100)
        var realSize = alert.getTxtSize(msg: msg, maxSize: maxSize)
        if realSize.width <= 100{
            realSize.width = 100
        }
        if realSize.height <= 50{
            realSize.height = 50
        }
        alert.msgLab.text = msg
        alert.borderSet(.white, 2)
        //不在正中央显示，偏下一点
        alert.frame = CGRect(x: (kScreenW-realSize.width)*0.5, y: (kScreenH-realSize.height)*0.5-70, width: realSize.width, height: realSize.height)
        alert.circular(5)
        let showBGView:UIView? = MyNavigator.getWindow() ?? MyNavigator.getTopVC()?.view
        if let window = showBGView{
            window.addSubview(alert)
            
            let txtCount = msg.count
            var delay = DispatchTime.now() + 2.5
            if txtCount > 30{
                delay = DispatchTime.now() + 4.5
            }else if txtCount > 50{
                delay = DispatchTime.now() + 6.0
            }
            DispatchQueue.main.asyncAfter(deadline:delay) {
                ToastView.sendNotify("kOnToastDismiss",nil)
            }
        }
    }
}
